package chapter5;

/**
 * wait() and notifyAll()
 * Created by LinGer on 2019/6/18
 */
public class NotifyAllTest extends Thread {

    private static Object obj = new Object();

    public NotifyAllTest(String name){
        super(name);
    }

    @Override
    public void run() {
        synchronized (obj){
            try {
                System.out.println(this.getName() + " wait");
                //唤醒当前的wait线程
                obj.wait();
                System.out.println(this.getName() + " continue");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        NotifyAllTest t1 = new NotifyAllTest("t1");
        NotifyAllTest t2 = new NotifyAllTest("t2");
        NotifyAllTest t3 = new NotifyAllTest("t3");

        t1.start();
        t2.start();
        t3.start();

        try {
            System.out.println(Thread.currentThread().getName() + " sleep 3s");
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        synchronized (obj){
            System.out.println(Thread.currentThread().getName() + " notifyAll");
            obj.notifyAll();
        }
    }
}
